package org.wyy.vm.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.wyy.vm.cache.VMCache;
import org.wyy.vm.dao.IVideoDao;
import org.wyy.vm.entity.Video;

/**
 * @author w
 *
 */
public class VideoDaoImpl implements IVideoDao {

	@Override
	public List<Video> searchByName(String name) {
		
		List<Video> resultList = new ArrayList<Video>();
		
		for (Video video : VMCache.VIDEO_LIST) {
			if (video.getName().contains(name)) {
				resultList.add(video);
			}
		}
		
		return resultList;
	}

	@Override
	public Video getById(long id) {
		
		for (Video video : VMCache.VIDEO_LIST) {
			if (video.getId() == id) {
				return video;
			}
		}

		return null;
	}

	@Override
	public void add(Video video) {
		VMCache.VIDEO_LIST.add(video);
		
		VMCache.saveVideo();
	}

	@Override
	public void delete(long id) {
		for (int i = 0; i < VMCache.VIDEO_LIST.size(); i++) {
			Video video = VMCache.VIDEO_LIST.get(i);
			if (video.getId() == id) {
				VMCache.VIDEO_LIST.remove(i);
				VMCache.saveVideo();
				break;
			}
		}
		
	}

	@Override
	public void update(Video video) {
		
		Video video2 = getById(video.getId());
		video2.setActors(video.getActors());
		video2.setName(video.getName());
		video2.setCoverImgPath(video.getCoverImgPath());
		video2.setContent(video.getContent());
		video2.setVideoPath(video.getVideoPath());
		
		VMCache.saveVideo();
		
	}


}
